public member function
<ios> <iostream>

std::basic_ios::operator bool

operator void*() const;
explicit operator bool() const;
Evaluate stream
Returns whether an error flag is set (either failbit or badbit).

Notice that this function does not return the same as member good, but the opposite of member fail.

The function returns a null pointer if at least one of these error flags is set, and some other value otherwise.
The function returns false if at least one of these error flags is set, and true otherwise.

Parameters

none

Return Value

A null pointer if at least one of failbit or badbit is set. Some other value otherwise.
true if none of failbit or badbit is set.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// evaluating a stream
#include <iostream>     // std::cerr
#include <fstream>      // std::ifstream

int main () {
  std::ifstream is;
  is.open ("test.txt");
  if (is) {
    // read file
  }
  else {
    std::cerr << "Error opening 'test.txt'\n";
  }
  return 0;
}

Data races

Accesses the stream object.
Concurrent access to the same stream object may cause data races.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the stream.

See also